package cn.com.nthotline.db;

import java.util.ArrayList;
import java.util.List;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import cn.com.nthotline.model.Area;
import cn.com.nthotline.util.CommonUtil;

public class DBArea implements DBInterface<Area> {
	private DatabaseHelper helper;

	public DBArea(Context context) {
		helper = new DatabaseHelper(context);
	}

	@Override
	public long insert(Area t) {
		// TODO Auto-generated method stub
		ContentValues values = new ContentValues();
		values.put(Area.ID, t.areaId);
		values.put(Area.NAME, t.areaName);
		values.put(Area.PARENTID, t.parentId);
		return helper.insertData(CreateDbConstants.TABLE_AREA, values);
	}

	@Override
	public List<Area> query(String id) {
		// TODO Auto-generated method stub
		List<Area> list = new ArrayList<Area>();
		SQLiteDatabase db = helper.getWritableDatabase();
		Cursor cursor = null;
//		if(!CommonUtil.isNumeric(id)) return null;
		
		if(null == id){
			cursor = db.rawQuery("select areaid,areaname from " + CreateDbConstants.TABLE_AREA, null);
			
		}else {
			cursor = db.rawQuery("select areaid,areaname from " + CreateDbConstants.TABLE_AREA + " where areaid = ?", new String[]{id});
			
		}
		while (cursor.moveToNext()) {
			Area area = new Area();
			area.areaId = cursor.getString(cursor.getColumnIndex(Area.ID));
			area.areaName = cursor.getString(cursor.getColumnIndex(Area.NAME));
			list.add(area);
		}
		return list;
	}
	

}
